<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
/*
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

CONNECTION DECONNECTION DES USER

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*/
//Demarrage session
	@session_start();


//Connection
if(@$_POST['action']=='login') {
	$pass = md5('jeton'.$_POST['password']);
	$query = 'SELECT id, nickname, lang FROM '.$GLOBALS['prefix'].'user Where nickname = ? AND password = ?';
	$ligne = ZzSQL::queryFirst($query, array($_POST['pseudo'],$pass));
	if($ligne != null) {
		//On creer les variables de session
			$_SESSION['user'] = $ligne['id'];
			$_SESSION['nickname'] = $ligne['nickname'];
			$_SESSION['lang'] = $ligne['lang'];

		//On ajoute les groupes
			ZzSessionGroup::reload();
			
		//On defini la prochaine actualisation des groupes
			if($GLOBALS['reloadGroupEvery'] >= 0)
				$_SESSION['nextTimestampToReloadGroups'] = time()+$GLOBALS['reloadGroupEvery'];

		//On génère le passCookie
			$passCookie = rand(0,9999999999);
		//On enregistre le passCookie
			ZzSQL::exec('UPDATE '.$GLOBALS['prefix'].'user SET passCookie = ? WHERE  id=?',array($passCookie,$_SESSION['user']));
		//On creer les cookies
			$expire = 365*24*3600;
			setcookie('user',$_SESSION['user'],time()+$expire);
			setcookie('passCookie',$passCookie,time()+$expire);
		//Redirection
			ZzJump::to(1);
		}
		else {
			loadZzS('ErrorLogin');
			exit();
		}
} 
//REconnection
elseif(@$_POST['action']=='reConnect') {
	//On récupère le pass cookie dans la base de donnée
		$ligne = ZzSQL::queryFirst('SELECT nickname, lang FROM '.$GLOBALS['prefix'].'user WHERE id= ? AND passCookie = ?', array(intval($_COOKIE['user']),$_COOKIE['passCookie']));
		//Le passCookie est bon, on creer les session et on change le pass
		if($ligne != null) {
			$_SESSION['user'] = $_COOKIE['user'];
			$_SESSION['nickname'] = $ligne['nickname'];
			$_SESSION['lang'] = $ligne['lang'];
			
		//On ajoute les groupes
			ZzSessionGroup::reload();

		//On defini la prochaine actualisation des groupes
			if($GLOBALS['reloadGroupEvery'] >= 0)
				$_SESSION['nextTimestampToReloadGroups'] = time()+$GLOBALS['reloadGroupEvery'];

		//On génère le passCookie
			$passCookie = rand(0,9999999999);
		//On enregistre le passCookie
			ZzSQL::exec('UPDATE '.$GLOBALS['prefix'].'user SET passCookie = ? WHERE  id=?', array($passCookie,$_SESSION['user']));
		//On creer les cookies
			$expire = 365*24*3600;
			setcookie('user',$_SESSION['user'],time()+$expire);
			setcookie('passCookie',$passCookie,time()+$expire);
		}
		else {
			$_POST['errorLogin'] = 'true';
		}
}
//Deconnection
elseif(@$_POST['action'] == 'logout') {
	//Suppresion des session
		$_SESSION['user'] = null;
		session_destroy();
	//Suppression des cookies
		//On regénère le passeCookie de la base de donnée
			$passCookie = rand(0,9999999999);
		//On enregistre le passCookie
			if(isSet($_COOKIE['user']))
				ZzSQL::exec('UPDATE '.$GLOBALS['prefix'].'user SET passCookie = ? WHERE  id=?', array($passCookie,$_COOKIE['user']));
		//On supprime le cookie
			setcookie('user','',time());
		//Redirection
			ZzJump::to(1);
}
?>